package io.github.uicdb.et.item.curios;

import com.google.common.collect.Multimap;
import io.github.uicdb.et.ModAttributes;
import net.minecraft.core.Holder;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.ItemStack;
import top.theillusivec4.curios.api.SlotContext;

import java.util.List;

public class DogeHappyItem extends CurioItem {
    public DogeHappyItem(Properties properties) {
        super(properties, 0);
    }

    @Override
    public void appendDefaultAttribute(Multimap<Holder<Attribute>, AttributeModifier> map, SlotContext slotContext, ResourceLocation id, ItemStack stack) {
        map.put(ModAttributes.HEALTH_GETTER, getSlotModifier(0, slotContext, 0.35, AttributeModifier.Operation.ADD_MULTIPLIED_BASE));
        map.put(ModAttributes.HEALTH_RECOVERY, getSlotModifier(1, slotContext, 0.2, AttributeModifier.Operation.ADD_MULTIPLIED_BASE));
        map.put(Attributes.ARMOR, getSlotModifier(2, slotContext, 0.5, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL));
    }

    @Override
    public void curioTick(SlotContext slotContext, ItemStack stack) {
        if (slotContext.entity().level().getGameTime() % 30 == 0) {//每1.5s计算一次防御转生命
            applyArmor2Health(slotContext);
        }
    }

    private static void applyArmor2Health(SlotContext slotContext) {
        var entity = slotContext.entity();
        if (entity.getAttributes().hasAttribute(Attributes.ARMOR) && entity.getAttributes().hasAttribute(Attributes.MAX_HEALTH)) {
            entity.getAttributes().getInstance(Attributes.MAX_HEALTH).addOrReplacePermanentModifier(
                    getSlotModifier(3, slotContext, entity.getAttributeValue(Attributes.ARMOR) * 0.25f, AttributeModifier.Operation.ADD_VALUE)
            );
        }
    }

    @Override
    public void onEquip(SlotContext slotContext, ItemStack prevStack, ItemStack stack) {
        super.onEquip(slotContext, prevStack, stack);
        applyArmor2Health(slotContext);
    }

    @Override
    public void onUnequip(SlotContext slotContext, ItemStack newStack, ItemStack stack) {
        super.onUnequip(slotContext, newStack, stack);
        var entity = slotContext.entity();
        AttributeMap map = entity.getAttributes();
        if (map.hasAttribute(Attributes.ARMOR) && map.hasAttribute(Attributes.MAX_HEALTH)) {
            var id = getSaftyID(slotContext, String.valueOf(3));
            if (map.hasModifier(Attributes.MAX_HEALTH, id)) {
                map.getInstance(Attributes.MAX_HEALTH).removeModifier(id);
            }
        }
    }

    @Override
    public void addAdditionTooltip(List<Component> tooltips, ItemStack stack) {
        tooltips.add(translate("doge_happy"));
    }
}
